<template>
  <div ref="dom" class="charts chart-line"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'ChartGauge',
  props: {
    text: String,
    gaugeData: Number,
    dataFormatter: String,
    max: Number,
    titleY:String
  },
  watch: {
    'gaugeData':function(newVal,oldVal){
      console.log(newVal)
      this.changeChart()
    }
  },
  mounted () {
    this.changeChart()
  },
  methods:{
    changeChart(){
      this.$nextTick(() => {
        let option = {
          title: {
            text: this.text,
            subtext: this.subtext,
            x: 'center',
            y:this.titleY
          },
          tooltip: {
            formatter: this.text + ': {c}%'
          },
          toolbox: {
            show: true,
            feature: {
              mark: {show: true},
              dataView: {show: true, readOnly: false},
              restore: {show: false},
              saveAsImage: {show: false}
            }
          },
          series: [
            {
              name: '当前数据值',
              type: 'gauge',
              center: ['50%', '55%'], // 默认全局居中
              detail: {
                formatter: this.dataFormatter,
                offsetCenter: ['0', '68%'],
                textStyle: {
                  fontSize: 24
                }
              },
              data: [{value: this.gaugeData, name: ''}],
              max: this.max,
              axisLine: { // 坐标轴线
                show: true, // 默认显示，属性show控制显示与否
                lineStyle: { // 属性lineStyle控制线条样式
                  color: [[0.2, 'lightgreen'], [0.4, 'orange'], [0.8, 'skyblue'], [1, '#ff4500']],
                  width: 20
                }
              },
              splitLine: { // 分隔线
                show: true, // 默认显示，属性show控制显示与否
                length: 20
              },
              pointer: {
                length: '60%',
                color: 'auto'
              }
            }
          ]
        }
        let dom = echarts.init(this.$refs.dom, 'tdTheme')
        dom.setOption(option)
      })
    }
  }
}
</script>

<style lang="less">

</style>
